Docker Kata 3: Container Volumes and File System
Learn how container volumes and file systems work.
Docker containers use an internal file system that’s isolated from the host and other containers. This kata will teach us what happens when we make changes to a container file system. We’ll also learn how to create volumes that can be shared between containers and their hosts and between other containers.
Step 1: Make container file system changes#
The command to stop and remove all the containers is given below.
Note: The container IDs we see will be different.
The output will be something like this:
Commands
Command/Parameter | Description |
| This stops all the containers. |
| This removes all the containers. |
The command to run the ubuntu container and execute the bash shell is given below.
After running the command above, the output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs the container in interactive mode. |
| This is the name of the image to run. |
| This is the command to run on the container. This is the |
This command runs an Ubuntu container and executes the Bash shell.
The command to create and list the file in the root of the container is given below.
The output will be something like this:
Commands
Command/Parameter | Description |
| This lists files in the container. |
| This writes text into a new file called |
| This lists files in the container. Note that the |
| This exits the |
This sequence of commands creates a new file in the root of the container called file1.txt. The ls command displays the file. The exit command terminates the bash program, which exits the container.
The command to run the second Ubuntu container is given below.
After running the command above, the output displayed will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs the container in interactive mode. |
| This is the name of the image to run. |
| This is the command to run on the container. This is the |
This command runs a second Ubuntu container and lists the files in the root. The ls command shows that the file1.txt file is not present in the second container. This is because the second container was a new instance of the ubuntu image. The Ubuntu image does not have the file1.txt file. New containers that are run from that image won’t have any changes made to other containers run from that image. The next steps in this kata will demonstrate how to share files between containers.
Step 2: Run a container with a named volume#
The command to mount a named volume to the container is given below.
The output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a container. |
| This runs a container in disconnected mode. |
| This assigns a name to a container |
| This is the name to assign to the container. |
| This mounts a volume to a container |
| This creates a new volume named |
| This is the name of the image to run. |
The Linux file system uses volumes. A volume is a logical file space, like a drive partition. Connecting a volume to the OS is referred to as mounting the volume.
This step demonstrates how to mount a named volume to a container. This step runs a new container, creates a new volume, and mounts the new volume to the container.
The -v paramter mounts a volume. This is followed by a value in the format [volume]:[path].
The name of the volume is myVolume, which is created if it doesn’t exist. The path in the container to which the volume is mounted is /webapp.
The command to list the running containers is given below.
We’ll see something like this when we run the command above:
Commands
Parameter | Description |
| This is the parent command. |
| This lists the running containers. |
This command lists the running containers.
The command to list all the files in the container is given below.
The output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This executes a command on a running container. |
| This is the name of the container on which to run the command. |
| This is the command to run. Note that there's a directory called |
This command lists all the files in the root of the web container. The /webapp folder is the mounted myVolume volume.
The command to list all the volumes is given below.
When we execute the command above, the output will be something like this:
Commands
Parameter | Description |
| This is the Docker |
| This lists all the |
This command lists all of the volumes, including the myVolume volume created in the first command.
We learned in the first step that changes made to a container file system aren’t persisted to the image. Volumes are used to create persistent stores that can be shared between containers or between the container and its host. This is useful in cases where multiple containers need to share the same data, such as a configuration database or application code.
Step 3: Share a volume between containers#
The command to run and mount the NGNIX container is given below.
The output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a command in a running container. |
| This runs a container in disconnected mode. |
| This assigns a name to the container. |
| This is the name to assign to the container. |
| This mounts a volume to the container. |
| The name of the volume to mount is |
| This is the container image to run. |
Docker volumes can be shared between multiple containers. This step runs a second NGINX container called web2 and mounts the myVolume volume to the container. There are now two NGINX containers running, both with the myVolume volume mounted to the path /webapp.
The command to create a file in the /webapp volume and mount the path is given below.
The output that will be displayed after running this command will be something like this:
Parameter | Description |
| This is the parent command. |
| This runs a command in a running container. |
| This is the name of the container on which to run the command. |
| This is the command to run in the container. This is the Bourne shell. |
| The |
| This is the inline script. This script writes text to a file inside the
|
This step runs a command inside the first container, web. The command creates a file in the /webapp volume mount path called file1.txt.
The command to list the files is given below.
The output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a command in a running container. |
| This is the name of the container on which to run the command. |
| This is the command to run. This lists the files in the |
The /webapp directory of both containers is the mounted myVolume volume. The file was created in the web container, yet now shows in the /webapp directory of both containers. This is because myVolume is shared between the containers. The file listed in the first ls command is the same as the file listed in the second.
Step 4: Run a container with a host-mounted volume#
The command to create a new dockervolume directory is given below.
The result of command above will be something like this:
Commands
Command/Parameter | Description |
| This creates a directory called |
| This is used to change to the |
| This creates two files in the |
| This lists the files in the |
| This changes back to the |
This sequence of commands creates a new dockervolume directory in the LVM, then creates two files (file1.txt and file2.txt) in that directory.
The command to run a new container with a host-mounted volume is given below.
When we execute the command above, the output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a container. |
| This runs a container in disconnected mode. |
| This assigns a name to a container. |
| This is the name to assign to the container. |
| This mounts a volume to the container. |
| This parameter mounts a directory on the host to a new volume in the container.
|
| This is the name of the image to run. |
This command runs a new container with a host-mounted volume. Docker mounts to the local host path $PWD/dockervolume. This is also called a bind mount.
The syntax for this command is similar to the previous steps. This command specifies a local path instead of a volume name. This mounts a local directory on the host (the LVM) to a directory in the container (/hostmounted).
The command to list the contents of the directory is given below.
The output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This executes a command in a running container. |
| This is the name of the container on which to run the command. |
| This is the command to run within the container. This lists the contents of the |
This command lists the contents of the /hostmounted directory in web3.
Note that the two files, file1.txt and file2.txt, are listed in the /hostmounted directory in the container. They are the same files as the ones in the dockervolume directory in the LVM. Any changes made to those files will be reflected in the LVM and in the container.
Note: Bind mounts can be useful in some cases, but there are trade-offs. A container that uses a bind mount depends on files and directories on the host, which can prevent container portability. Docker recommends using shared volumes instead of bind mounts to avoid dependencies between containers and hosts.
Practice commands#
We’ve given a terminal and table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!
Commands
Step | Command |
This stops and removes all the containers and images. |
|
This runs a container from the |
|
This lists the files in the |
|
This writes the text “file1text” to a file called |
|
This lists the files in the root folder again, allowing us to locate the file named |
|
This exits the container. |
|
This runs a second container interactively from the |
|
This lists the files in the root (and check to see if |
|
This exits the container. |
|
This lists all the containers (running and stopped). |
|
This runs a container named |
|
This lists all the running containers. |
|
This executes the |
|
This lists all the volumes. |
|
This runs a container named |
|
This executes a command on the |
|
This executes a command to list the contents of |
|
This executes a command to list the contents of |
|
This makes a directory called |
|
This write two files, |
|
This lists the contents of |
|
This changes back to the parent directory. |
|
This runs an NGINX container named |
|
This executes a command on |
|
Docker Kata 2: Disconnected Containers
Docker Kata 4: Running a Web Server in a Container